package com.iweb.util;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class Regist extends HttpServlet {

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //校验用户是否存在
        String username = req.getParameter("username");
        String sql = "select count(*) cou from users " + "where username = ?";
        List<Map<String,Object>> res = JdbcUtil.jdbcBySelect(sql,username);
        Map<String,Object> map = res.get(0);
        Long cou = (Long) map.get("cou");
        if(cou > 0){
            //用户存在
            resp.getWriter().write("用户已存在");
        }else{
            //用户不存在
            String password = req.getParameter("password");
            String email = req.getParameter("email");
            String sql2 = "insert into my_user (username,password,email) values (?,?,?)";
            int i = JdbcUtil.jdbcByUpdate(sql2,username,password,email);
            resp.getWriter().write("注册成功");
        }
    }
}
